package com.yxt.service;

import java.util.ArrayList;
import java.util.List;

import com.yxt.dao.DBOperator;
import com.yxt.model.Favirote;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FavoriteService {

	@SuppressWarnings("unused")
	private Context context;
	private DBOperator dbHelper;

	public FavoriteService(Context context) {
		dbHelper = new DBOperator(context);
	}

	public void saveFavirote(Favirote favirote) {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("insert into favorite (code , name , type , addr,river) values (? , ? , ? , ?, ?)",
		new Object[] { favirote.getCode(), favirote.getName(), favirote.getType(), favirote.getAddr(), favirote.getRiver()});
	}
	
	public void updateFavirote(Favirote favirote) {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("update favorite set name = ? , type = ? , addr = ? , river = ? where code = ? ",
		new Object[] {favirote.getName(), favirote.getType(), favirote.getAddr(), favirote.getRiver(),favirote.getCode()});
	}
	
	public void saveOrUpdateFavirote(Favirote favirote) {
		Favirote fav = findFaviroteByCode(favirote.getCode());
		if (fav == null) {
			saveFavirote(favirote);
		} else {
			updateFavirote(favirote);
		}
	}

	public Favirote findFaviroteByCode(String code) {
		Favirote favirote = null;
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select code , name , type , addr, river, orderNum from favorite where code = ? ",
				new String[] { String.valueOf(code) });
		
		if (cs.moveToNext()) {
			int index = 0;
			favirote = new Favirote();
			favirote.setCode(cs.getString(index++));
			favirote.setName(cs.getString(index++));
			favirote.setType(cs.getString(index++));
			favirote.setAddr(cs.getString(index++));
			favirote.setRiver(cs.getString(index++));
		}
		cs.close();
		return favirote;
	}
	
	public List<Favirote> findFaviroteList() {
		List<Favirote> list = new ArrayList<Favirote>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select code , name , type , addr, river, orderNum from favorite ",null);		
		while (cs.moveToNext()) {
			int index = 0;
			Favirote favirote = new Favirote();
			favirote.setCode(cs.getString(index++));
			favirote.setName(cs.getString(index++));
			favirote.setType(cs.getString(index++));
			favirote.setAddr(cs.getString(index++));
			favirote.setRiver(cs.getString(index++));
			list.add(favirote);
		}
		cs.close();
		return list;
	}

	public List<Favirote> findFaviroteListByType(String type) {
		List<Favirote> list = new ArrayList<Favirote>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select code , name , type , addr, river, orderNum from favorite where type = ? ", new String[] { type });		
		while (cs.moveToNext()) {
			int index = 0;
			Favirote favirote = new Favirote();
			favirote.setCode(cs.getString(index++));
			favirote.setName(cs.getString(index++));
			favirote.setType(cs.getString(index++));
			favirote.setAddr(cs.getString(index++));
			favirote.setRiver(cs.getString(index++));
			list.add(favirote);
		}
		cs.close();
		return list;
	}
	
	public void delete(String code) {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("delete from favorite where code = ? ", new Object[] {code});
	}

	public void closeDB() {
		dbHelper.close();
	}

}
